package zcw.com.lib_leet_code;

/**
 * Created by 朱城委 on 2021/10/12.<br><br>
 *
 * 中等：矩形面积
 */
public class Topic223 {

    public static void main(String[] args) {
        Topic223 instance = new Topic223();

        int[] array = new int[] {-3, 0, 3, 4, 0, -1, 9, 2};
        System.out.println(instance.computeArea(array[0], array[1], array[2], array[3], array[4], array[5], array[6], array[7]));

        array = new int[] {-2, -2, 2, 2, -2, -2, 2, 2};
        System.out.println(instance.computeArea(array[0], array[1], array[2], array[3], array[4], array[5], array[6], array[7]));
    }

    public int computeArea(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) {
        int widthA = ax2 - ax1;
        int heightA = ay2 - ay1;
        int areaA = widthA * heightA;

        int widthB = bx2 - bx1;
        int heightB = by2 - by1;
        int areaB = widthB * heightB;

        int mulX1 = Math.max(ax1, bx1);
        int mulY1 = Math.max(ay1, by1);

        int mulX2 = Math.min(ax2, bx2);
        int mulY2 = Math.min(ay2, by2);

        if(mulX2 > mulX1 && mulY2 > mulY1) {
            return areaA + areaB - (mulX2 - mulX1) * (mulY2 - mulY1);
        }
        else {
            return areaA + areaB;
        }
    }
}
